Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
Angular - getting params from route.url sometimes results in "undefined" text

I have a question.

I found a weird bug that I can't reproduce on my machine. My app has a flow like this:

  1. User clicks a short url, calls backend (nodejs) and the backend will do a redir to angular frontend. For example to: (https://mysite/#/actpost/param1/param2)
  2. Every time the backend does a redir, I put it in log.txt, I have checked and they all seem to be correct, redirecting to the correct frontend urls (no "undefined string" in the url)
  3. In frontend, it will do url split to get the params and post a request to backend with said params, I also log it in log.txt during every post call (in backend side) The code to get params is as follows :
ngOnInit(): void {
   this.param1= this.router.url.split('/')[2];
   this.param2 = this.router.url.split('/')[3];

   //Do a POST request to backend
   apiCall(param1, param2);

I then check the post log and this is where I found the bug, sometimes param1 in the request body contains the string "undefined" rather than the actual string from in the url (param1).

Which leads me to this question: is the router function async? I have also filled both param1 & param2 with empty string before trying to get params from URL, so I don't know how it can get filled with "undefined" string. I have also tried getting params via activated Route paramMap and the same problem still happens.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The router is asynchronous, below a code example for getting URL parameters.

  private ngOnInit(): void {
    this.route.queryParams.pipe(
      map(params => [params['id1'],params['id2']]),
      tap(([a1,a2]) => {console.log("A1:",a1,"A2:",a2)}),
      catchError((err) => {
        return throwError(()=>new Error(`Error : ` + err))
      })
    ).subscribe({next: ([a1,a2])=>{apiCall(a1,a2)}});
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda